<?php
/**
 * Created by PhpStorm.
 * User: hpp
 * Date: 2019/12/6
 * Time: 10:54
 */

namespace api\task\controller;

use think\Controller;
use think\Db;

use api\common\service\CurlService;
use api\common\service\FunctionService;


use api\common\model\RlHiseServiceProjectModel;
use api\common\model\RlEtCustomerModel;
use api\common\model\RlEtAzsModel;
use api\common\model\RlEtAzdModel;

/**
 * 数据同步接口
 * Class TaskController
 * @package api\task\controller
 */
class TaskgzhController extends Controller
{


    /*项目列表数据
      param[
        手机号，
        sign，
      ]
    */
    public function itemlist()
    {
        header('Access-Control-Allow-Origin:*');
        //安全验证
        $param = input('param.');
        $result = $this->cssEncrypt($param);
        if(!$result)
        {
            return json([
                "msg"=>'安全验证失败',
                "code"=>'0',
            ]);
        }
        //获取项目列表
        $sj_one = $param['sj_one'];
        if(!$sj_one)$this->error('参数错误');
        $rlHiseServiceProjectModel = new RlHiseServiceProjectModel();

        $hise_service_project = $rlHiseServiceProjectModel->where('zzdpdzt','<>',0)->where('sj_one',$sj_one)->select()->toArray();
        foreach ($hise_service_project as $key => $value) {
                if($value['wgsj'] && $value['wgsj']<(time()-86400*7))
                {
                    // unset($hise_service_project[$key]);
                    // 超过七天未评价，公众号不能评价
                    $hise_service_project[$key]['sfpj'] = 1;
                }else{
                    $hise_service_project[$key]['qx_name'] = Db::name('rl_hise_qx_table')->where('qx_code',$hise_service_project[$key]['qx'])->field('qx_name')->find()['qx_name'];
                $hise_service_project[$key]['city_name'] = Db::name('rl_hise_dq_table')->where('dq_code',$hise_service_project[$key]['city'])->field('dq_name')->find()['dq_name'];
                $hise_service_project[$key]['provice_name'] = Db::name('rl_hise_area')->where('sf_code',$hise_service_project[$key]['provice'])->field('sf_name')->find()['sf_name'];
                }
            }
        return json([
            "data"=>$hise_service_project,
            "msg"=>'项目信息同步成功',
            "code"=>1,
        ]);
    }


    //报告书
    public function report(){
        header('Access-Control-Allow-Origin:*');
        $param = input('param.');
        $result = $this->cssEncrypt($param);
        if(!$result)
        {
            return json([
                "msg"=>'安全验证失败',
                "code"=>'0',
            ]);
        }
        $service_project_id = $param['service_project_id'];
        if(!$service_project_id)$this->error('参数错误');
        $data  =  $this->PrepositionReport($service_project_id);
        $this->success('请求成功','',$data);
    }


    //报告书前置
    public function  PrepositionReport($service_project_id)
    {
        //多表引入查询方法
        $info = RlHiseServiceProjectModel::Where('hise_service_project_id',$service_project_id)->field('azd_id,yh_name,xb,sj_one,provice,city,qx,address,qyfgs,jxs
        ,azs,azd,fgsfwfzr,cplb,yyjc_sj,jkh,zjbxq,zybjbxq,jxsxsy,bz,pic_one,pic_two,wgsj')->find();
//        if(!$info) $this->error('该用户不存在');
        if(empty($info['qx'])){
            $city = Db::name('rl_hise_dq_table')->where('dq_code',$info['city'])->field('dq_name')->select()['dq_name'];
            $provice = Db::name('rl_hise_area')->where('sf_code',$info['provice'])->field('sf_name')->find()['sf_name'];
            $info['address_detail'] = $provice.$city.$info['address'];
            $info['address'] = $provice.$city;
        }else{
            $xq = Db::name('rl_hise_qx_table')->where('qx_code',$info['qx'])->field('qx_name')->find()['qx_name'];
            $city = Db::name('rl_hise_dq_table')->where('dq_code',$info['city'])->field('dq_name')->find()['dq_name'];
            $provice = Db::name('rl_hise_area')->where('sf_code',$info['provice'])->field('sf_name')->find()['sf_name'];
            $info['address_detail'] = $provice.$city.$xq.$info['address'];
            $info['address'] = $provice.$city.$xq;
        }
        $info['jxs_name'] = RlEtCustomerModel::where('jxsbm',$info['jxs'])->field('jxs_mc')->find()['jxs_mc'];
        $info['azs_name'] = RlEtAzsModel::where('server_code',$info['azs'])->field('server_name')->find()['server_name'];
        $info['azd_name'] = RlEtAzdModel::where('engineer_code',$info['azd'])->field('engineer_name')->find()['engineer_name'];
        $info['fgs_name'] = Db::name('rl_company')->where('orgdept',$info['qyfgs'])->field('orgname')->find()['orgname'];
        $info['yyjc_sj']  = date('Y-m-d', $info['yyjc_sj']);
        $info['jxs_name'] = RlEtCustomerModel::where('jxsbm',$info['jxs'])->field('jxs_mc')->find()['jxs_mc'];
        $prefix = 'cmf_';
        $join = [
            ["{$prefix}rl_node node", 'node.node_id = pro.azjd'],
        ];
        $list = Db::name('rl_hise_service_pro_process')->alias('pro')
            ->join($join)
            ->where('pro.gc_azdid',$info['azd_id'])->order('node.sort desc')
            ->field('pro.product_type,pro.jdqrsj,pro.cplp,pro.azjd,pro.tp_one,pro.tp_two,pro.tp_there,node.node_name')
            ->select()->toArray();

            $arr = [1=>'多联机',2=>'单元机',3=>'地暖机',4=>'新风类'];
        $product_type = [];
        foreach ($list as $key => $value) {
            if(!in_array($value['product_type'], $product_type))
            {
                $product_type[] = $value['product_type'];
            }
        }
        $newlist = [];
        foreach ($product_type as $key => $value) {
            $newlist[$key]['product_name'] = $arr[$value];
            $newlist[$key]['product_type'] = $value;
        }
        foreach ($newlist as $key => $value) {
            foreach ($list as $k => $v) {
                if($value['product_type'] == $v['product_type'])
                {
                    $newlist[$key]['node_list'][] = $v;
                }
            }
        }
        $data['info'] = $info;
        $data['node_list'] = $newlist;
        return $data;
    }

    //评价接受接口
    public function evaluate()
    {
        header('Access-Control-Allow-Origin:*');
        //安全验证
        $param = input('param.');
        // trace(json_encode($param),'info');
        $result = $this->cssEncrypt($param);
        if(!$result)
        {
            return json([
                "msg"=>'安全验证失败',
                "code"=>'0',
            ]);
        }
        //保存评价信息
        $data['yh_npsfz'] = $param['mark'];
        $data['pjx'] = $param['content_id'];
        $data['pjnr'] = $param['content'];
        $data['hise_service_project_id'] = $param['service_project_id'];
        $data['sfpj'] = 1;
        if($param['link_id'])
        {
            $link_id = explode(',',$param['link_id']);
            foreach ($link_id as $key => $value) {
                if($key==0)
                {
                    $data['zp_one'] = $value;
                }
                if($key==1)
                {
                    $data['zp_two'] = $value;
                }
                if($key==2)
                {
                    $data['zp_three'] = $value;
                }
            }
        }
        
        if(!$data['yh_npsfz']||!$data['pjx']||!$data['hise_service_project_id']){
            return json([
            "msg"=>'请选择分数和评价项',
            "code"=>0,
            ]);
        }
        $result = RlHiseServiceProjectModel::where('hise_service_project_id',$data['hise_service_project_id'])->update($data);
        if($result)
        {
            return json([
            "msg"=>'评价成功',
            "code"=>1,
            ]);
        }else{
            return json([
            "msg"=>'评价失败',
            "code"=>0,
            ]);
        }
    }
        /*
     * 校验签名加密方法
     * @param array $query_param 请求参数数组
     * return string $sign 返回加密后的签名
     */
    private function cssEncrypt($query_param){
        ksort($query_param);
        $sign = $query_param['sign'];
        unset($query_param['sign']);
        //加入sign_key
        $tmp_sign = md5(implode($query_param) . md5('_/.(2;l@tx~XvF)`4aZC:f+Q1h<b*&pN%dms#g=q!@#$%^'));
        $tmp_sign = substr($tmp_sign,0,24);
        if($sign === $tmp_sign)
        {
            return true;
        }else{
            return false;
        }
    }


}